package com.leenmvc.core.security.dao.core;

import com.leenmvc.core.base.BaseBusinessDao;
import com.leenmvc.core.dao.communal.Wrapper;
import com.leenmvc.core.security.entity.core.SysLanguage;
import com.leenmvc.core.security.entity.core.SysMenu;
import com.leenmvc.core.utils.http.HttpContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SysMenuDao extends BaseBusinessDao<SysMenu> {

    @Autowired
    private SysLanguageDao sysLanguageDao;

    /**
     * 重写getPageList
     *
     * @param wrapper
     * @return
     */
    public List<SysMenu> getPageList(Wrapper wrapper) {
        List<SysMenu> pageList = getPageList(wrapper);
        for(SysMenu menu: pageList) {
            Wrapper queryWrapper = getQueryWrapper(SysLanguage.class);
            queryWrapper.eq("table_id", menu.getId());
            queryWrapper.eq("language", HttpContextUtils.getLanguage());
            queryWrapper.incField("fieldValue");
            menu.setName(sysLanguageDao.getOneField(queryWrapper));
        }
        return pageList;
    }


    /**
     * 获取父级菜单
     * @param sysMenu
     * @return
     */
    public SysMenu getParentMenu(SysMenu sysMenu){
        Long pid = sysMenu.getPid();
        SysMenu parent = get("id", pid);
        return parent;
    }

}
